import java.util.*;
import java.io.*;

//library for weka
import weka.core.Instances;
import weka.core.Instance;
//import weka.classifiers.trees.J48;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.trees.Id3;
import weka.classifiers.trees.J48;
import weka.classifiers.trees.NBTree;
import weka.classifiers.*;
import weka.classifiers.trees.j48.*;
import weka.classifiers.Evaluation;

public class DecisionTreeInferEngine {
	
	public void runDecisionTreeInferEngine(String trainFile) throws Exception{
		try {
			BufferedReader reader, testData;
			reader = new BufferedReader(new FileReader(trainFile));
			//testData = new BufferedReader(new FileReader(testFile));
			Instances data, test;
			// build the train and test date instances
			data = new Instances(reader);
			//test = new Instances(testData);

			reader.close();
			//testData.close();

			data.setClassIndex(data.numAttributes() - 1);
			//test.setClassIndex(test.numAttributes() - 1);
			
			
			//Instance for weather.arff
//			Instance iTest = new Instance(5);
//
//			System.out.println(data.attribute(0));
//			iTest.setValue(data.attribute(0), "sunny");
//			iTest.setValue(data.attribute(1),85);
//			iTest.setValue(data.attribute(2),85);
//			iTest.setValue(data.attribute(3), "FALSE");
//			 //iTest.setValue(data.attribute(4),0);
	//////////////////////////////////////////////
			
			/*Instance for restaurantChoose.arff*/
//			Instance iTest = new Instance(11);
//
//			//System.out.println(data.attribute(0));
//			iTest.setValue(data.attribute(0), "yes");
//			iTest.setValue(data.attribute(1), "no");
//			iTest.setValue(data.attribute(2), "no");
//			iTest.setValue(data.attribute(3), "yes");
//			iTest.setValue(data.attribute(4), "some");
//			iTest.setValue(data.attribute(5), "H");
//			iTest.setValue(data.attribute(6), "no");
//			iTest.setValue(data.attribute(7), "yes");
//			iTest.setValue(data.attribute(8), "F");
//			iTest.setValue(data.attribute(9), "vs");

			
//			Instance iTest = new Instance(3);
//
//			System.out.println(data.attribute(0));
//			iTest.setValue(data.attribute(0), 1.5);
//			iTest.setValue(data.attribute(1), 1.5);
			
			
			/*** Instance for testRes.arff*/
			Instance iTest = new Instance(7);

			//System.out.println(data.attribute(0));
			iTest.setValue(data.attribute(0), 3);
			iTest.setValue(data.attribute(1), 7);
			iTest.setValue(data.attribute(2), 20);
			iTest.setValue(data.attribute(3), 9);
			iTest.setValue(data.attribute(4), 10);
			iTest.setValue(data.attribute(5), "china");
			
			// use the data's type
			iTest.setDataset(data);
			System.out.println(iTest.dataset().toString());
			
			// build the J48 tree
			J48 tree = new J48();
			tree.buildClassifier(data);
			
			// build the Id3 tree
//			Id3 tree = new Id3();
//			tree.buildClassifier(data);
		
			//build the Naive Bayes Tree
//			NBTree tree = new NBTree();
//			tree.buildClassifier(data);
			
			//BayesNet
//			BayesNet tree = new BayesNet();
//			tree.buildClassifier(data);
			//System.out.println(tree.graph());
			

			
			System.out.println(tree.toString());
			double result;
			result = tree.classifyInstance(iTest);
			System.out.println("Results = " + result);
			// evaluate classifier and print some statistic
//			Evaluation eval = new Evaluation(data);
//			eval.evaluateModel(tree, test);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
}
